/**
 * https://leetcode.cn/problems/baseball-game/
 */
function calPoints(ops: string[]): number {
  let ans: number[] = [];

  for (let ope of ops) {
    const len = ans.length - 1;
    switch (ope) {
      case "+":
        ans.push(ans[len - 1] + ans[len]);
        break;
      case "C":
        ans.pop();
        break;
      case "D":
        ans.push(ans[len] << 1);
        break;
      default:
        ans.push(+ope);
    }
  }

  return ans.reduce((total, current) => total + current, 0);
}
